package server;


import java.net.*;
import java.io.*;

import shared.SerialData;


	public class ATMTCPServer {
	    public void runServer() {
		try{
			int serverPort = 7896; 
			ServerSocket listenSocket = new ServerSocket(serverPort);
			while(true) {
				Socket clientSocket = listenSocket.accept();
				Connection c = new Connection(clientSocket);
			}
		} catch(IOException e) {System.out.println("Listen :"+e.getMessage());}
	    }
	}

	
	//inner class
	class Connection extends Thread {
		ObjectInputStream in;
		ObjectOutputStream out;
		Socket clientSocket;
		public Connection (Socket aClientSocket) {
		    try {
		    	
			clientSocket = aClientSocket;
			in = new ObjectInputStream( clientSocket.getInputStream());
			out =new ObjectOutputStream( clientSocket.getOutputStream());
			this.start();
		     } catch(IOException e)  {
		    	 System.out.println("Connection:"+e.getMessage());
		     }
		}
		
		public void run(){
		    try {			   
		    	// receiving SerialData object over TCP
		    	SerialData sd = (SerialData) in.readObject();
		    	System.err.println("SerialData object received.");
		    	//String data = in.readUTF();
		    	sd.userExist = true;
		    	
		    	out.writeObject(sd);
		    } catch(EOFException e) {
		    	System.out.println("EOF:"+e.getMessage());
		    } catch(IOException e) {
		    	System.out.println("IO:"+e.getMessage());
		    } catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} finally{ 
		    	try {
		    		clientSocket.close();
		    		}catch (IOException e){
		    			/*close failed*/
		    			}
		    		}
		}
	}

